package com.vma.manageSystem.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.vma.advertisement.model.Advertisement;
import com.vma.advertisement.model.Message;
import com.vma.manageSystem.util.ReturnsUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by chenzui on 2017/3/29.
 */
public class MessageController extends Controller {

    public void qryMessageList(){
        String status = getPara("status");
        String title = getPara("title");
        String start = getPara("start");
        String end = getPara("end");

        int pageNo = getParaToInt("cPage");
        int pageSize = getParaToInt("pSize");

        List<String> param = new ArrayList<String>();
        String condition = "";

        if(status != null && !"".equals(status)){
            condition += "and a_status = ? ";
            param.add(status);
        }

        if(title != null && !"".equals(title)){
            condition += "and title like ? ";
            param.add("%"+title+"%");
        }
        if(start != null && !"".equals(start)){
            condition += "and create_time > ? ";
            param.add(start);
        }
        if(end != null && !"".equals(end)){
            condition += "and create_time < ? ";
            param.add(end);
        }

        Page<Message> advertisementPage = Message.dao.paginate(pageNo,pageSize,"select * ","from message where if_delete = 0 "+condition+
                        " order by create_time desc"
                ,param.toArray());
        renderJson(ReturnsUtil.returnSuccess(advertisementPage));
    }


    public void add(){
        String content = getPara("content");
        String url = getPara("url");
        Message message = new Message();
        if(url != null && !"".equals(url)){
            message.setUrl(url);
            message.setIfLink("1");
        }else {
            message.setIfLink("0");
        }
        message.setContent(content);
        message.setAStatus("1");
        message.setIfDelete("0");
        message.setCreateTime(new Date());
        message.save();
        renderJson(ReturnsUtil.returnSuccess("成功"));
    }

    public void update(){
        try{
            String id = getPara("id");
            String status = getPara("status");
            Message.dao.findById(id).setAStatus(status).update();
            renderJson(ReturnsUtil.returnSuccess("成功"));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void getMessage(){
        try{
            String id = getPara("id");
            renderJson(ReturnsUtil.returnSuccess(Message.dao.findById(id)));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void delete(){
        try{
            String id = getPara("id");
            Message.dao.findById(id).setIfDelete("1").update();
            renderJson(ReturnsUtil.returnSuccess("成功"));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
